import { ref, unref, watch, onMounted } from 'vue'
import { getSelection, batchUpdateValue,fireExecute } from '/src/utils/index'
import { post } from "/src/api/index";
import { sortObject } from '/src/utils/index'
export function init(self) {
  const module = self.module.value
  self.extra_info_columns = []
  self.title = ref('workspace_config')
  self.custom = {
    columns : [
      {
        prop: 'id',
        hide: true,
      },
      {
        prop: 'workspace_name',
        order: 'asc',
        width: 150,
      },
      {
        prop: 'name',
        order: 'asc',
        width: 300,
      },
      {
        prop: 'value',
        width: 400,
      }
    ],

    editColumns : [
      {
        prop: 'id',
        hide: true,
      },
      {
        prop: 'workspace_name',
        label: '工作区',
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
        required: true,
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          if (mode.indexOf('init') < 0) {
          }
        },
      },
      {
        prop: 'name',
        required: true,
      },
      {
        prop: 'value',
        type: 'textarea',
        rows: 5,
        width: 400,
      },
      {
        prop: 'extra_info_common_workspace_config_',
        label: '通用扩展属性',
        type: 'extra_info_common_workspace_config_',
        buttons: [
          {
            label: '删除',
            onclick: param => {
              let {form} = param
              if(form.extra_info_common_workspace_config__k) {
                let extra_info = JSON.parse(form.extra_info||'{}')
                delete extra_info[form.extra_info_common_workspace_config__k]
                form.extra_info = JSON.stringify(sortObject(extra_info), null, 2)
              }
            }
          },
          {
            label: '新增',
            onclick: param => {
              let {form} = param
              if(form.extra_info_common_workspace_config__k) {
                let extra_info = JSON.parse(form.extra_info||'{}')
                extra_info[form.extra_info_common_workspace_config__k] = form.extra_info_common_workspace_config__v
                form.extra_info = JSON.stringify(sortObject(extra_info), null, 2)
              }
            }
          },
        ],
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          if (mode.indexOf('init') < 0) {
            if(form.extra_info_common_workspace_config__k) {
              let extra_info = JSON.parse(form.extra_info||'{}')
              extra_info[form.extra_info_common_workspace_config__k] = form.extra_info_common_workspace_config__v
              
              form.extra_info = JSON.stringify(sortObject(extra_info), null, 2)
            }
          }
        },
      },
      {
        prop: 'extra_info',
        type: 'textarea',
        rows: 5,
        onclick: param => {
          let txt = unref(unref(self).editDialogInfo).form.extra_info
          let target = param.srcElement
          if('selectionStart' in target) {
            let leftStr = txt.substr(0, target.selectionStart)
            let line = leftStr.split('\n').length-1
            let array =  txt.split('\n')
            let leftKey = array[line].split(':')[0]
            leftKey = JSON.parse(leftKey)
            if(leftKey) {
              let extra_info = JSON.parse(unref(unref(self).editDialogInfo).form.extra_info)
              unref(unref(self).editDialogInfo).form.extra_info_common__k = leftKey
              unref(unref(self).editDialogInfo).form.extra_info_common__v = extra_info[leftKey]
            }
          }
        },
      },
    ],

    queryColumns : [
      {
        prop: 'workspace_name',
        fixed: true,
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
      },
      {
        prop: 'name_like',
        label: '名称',
        width: '150px',
      },
      {
        prop: 'value_like',
        label: '值',
        width: '150px',
      },
    ],

    buttons: [
      {
        prop: 'batchEdit',
        label: '批量编辑',
        loading: false,
        onclick: (param) => {
          let {batchEditDialogInfo : dialogInfo, selectRows} = self
          dialogInfo = unref(dialogInfo)
          selectRows = unref(selectRows)
          if (!selectRows || selectRows.length == 0) {
            ElMessage.error(`请选择至少一条记录后再操作`);
            return;
          }

          selectRows = unref(selectRows)
          let item = selectRows[0]
          dialogInfo.form = {...item}
          dialogInfo.form_ori = {...item}
          dialogInfo.show({...param, dialogInfo})
        }
      },
      {
        prop: 'fireExecute',
        label: '生成文件',
        type: 'success',
        loading: false,
        onclick: (param) => {
          fireExecute({param, self})
        }
      },
      {
        prop: 'fireExecuteAi',
        label: '生成文件(ai)',
        loading: false,
        onclick: (param) => {
          fireExecute({param, self, isAi:true})
        }
      },
    ],

    remoteConfig : {
    },

    onInit: () => {
      self.handleSearch_ori = self.handleSearch
      self.handleSearch = (param) => {

        let projects, configs, extra_info_common_workspace_config__ks
        return post('sdp_project', 'list', {
          workspace_name:unref(self.query).workspace_name,
          query_options: { order_by: [ { name: 'asc', } ] },
        })
        .then(data => {
          projects = data
          return post('sdp_workspace_config', 'list', {
            workspace_name:unref(self.query).workspace_name,
            query_options: { order_by: [ { name: 'asc', } ] },
          })
        })
        .then(data => {
          configs = data
          extra_info_common_workspace_config__ks = configs.filter(ele => ele.name.indexOf('extra_info_workspace_config_') == 0).map(ele => ({label: ele.value, value: ele.name.substring('extra_info_workspace_config_'.length)}))
          return self.handleSearch_ori(param)
        })
        .then( data => {
          unref(self.tableData).forEach( ele => {
            if(ele.project_names) {
              ele._project_names = ele.project_names.split(',')
              ele.extra_info_common_workspace_config__s = ele._project_names.map(ele => ({label: ele, value: ele}))
            } else {
              ele.extra_info_common_workspace_config__s = projects.map(ele => ({label:ele.name, value: ele.name}))
            }
            ele.extra_info_common_workspace_config__ks = extra_info_common_workspace_config__ks
            if (ele.extra_info) {
              let j = JSON.parse(ele.extra_info)
              for(let p in j) {
                ele['extra_info_'+p] = j[p]
              }
              ele.extra_info = JSON.stringify(j, null, 2)
            }
          })
          return data;
        })
      }
    },
  }
}

